package appx.craft.droid.uc;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;
import appx.craft.droid.R;
import appx.craft.droid.bean.FeedBean;
import appx.craft.droid.utils.Log;

/**
 * 
 * This class is used to show pop up window in sliding screen top bar 
 *
 */
public class ActivityTopMenu implements OnClickListener {
	
	private String TAG = ActivityTopMenu.class.getCanonicalName();
	
	private Context mContext = null;
	private LayoutInflater mLInflater = null;
	private PopupWindow mPopupWindow = null;
	
	private boolean mIsShowing = false;
	private Activity mActivity;
	
	private TextView textView_topText;
	private Button btn_done;
	
	private OnActivityMenuListener mMenuCallBack;
	
	public boolean isShowing() { return mIsShowing; }
	
	
	public ActivityTopMenu(Context context,LayoutInflater mLayoutInflater){
		Log.d(TAG,"[ Activity Top Menu ]");
		mContext = context;
		mLInflater = mLayoutInflater;
		mActivity = (Activity)context;
		mMenuCallBack = (OnActivityMenuListener)context;
	}
	
	public synchronized void show(View v,int mPosition,ArrayList<FeedBean> arryListFeed){
		Log.d(TAG,"[ show ] ");
		mIsShowing = true;
		if (mPopupWindow != null) return; //already showing
		Display mDisplay = ((WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
		
		View mView= mLInflater.inflate(R.layout.custom_top_menu, null);
		
		btn_done = (Button)mView.findViewById(R.id.btn_done);
		btn_done.setOnClickListener(this);
		
		textView_topText = (TextView)mView.findViewById(R.id.textview_title_header);
		setPageSize(mPosition, arryListFeed);
		
		mPopupWindow = new PopupWindow(mView,android.view.ViewGroup.LayoutParams.MATCH_PARENT,65, false);
        mPopupWindow.setAnimationStyle(android.R.style.Animation_Dialog);
    	mPopupWindow.setWidth(mDisplay.getWidth());
    	mPopupWindow.showAtLocation(v, Gravity.TOP,0, 50);
    	
	}
	
	public synchronized void hide() {
		mIsShowing = false;
		if (mPopupWindow != null) {
			mPopupWindow.dismiss();
			mPopupWindow = null;
		}
		return;
	}
	
	public void setPageSize(final int mPosition,final ArrayList<FeedBean> arrListImageBean){
		
		if(isShowing()){
			
			Log.d(TAG,"[ TOP NUMBER ] " + mPosition);
			
			mActivity.runOnUiThread(new Runnable() {
				
				public void run() {
					textView_topText.setText(new StringBuilder().append((mPosition+1)).append(" of ").append(arrListImageBean.size()));
				}
			});
		}
		
		
	}


	@Override
	public void onClick(View v) {
		mMenuCallBack.closeMenu();
	}
}
